package org.coral.server.game.module.artifact.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.coral.server.core.server.AbstractModuleMultiDomain;

import org.coral.server.core.event.IEvent;
import org.coral.server.core.server.AbstractModuleMultiDomain;
import org.coral.server.game.data.proto.PBBag.PBMissionInfo;
import org.coral.server.game.helper.log.NatureEnum;
import org.coral.server.game.helper.result.ErrorCode;
import org.coral.server.game.module.mission.handler.ArtifactMissionHandler;
import org.coral.server.game.module.mission.handler.IMissionHandler;
import org.coral.server.game.module.mission.type.IMission;

/**
* ArtifactDomain
* @author Jeremy
*/
	interf = "implements"

public class ArtifactDomain extends AbstractModuleMultiDomain<Long, Long, Artifact>  IMissionHandler,{

	private static final Logger log = LoggerFactory.getLogger(ArtifactDomain.class);
	
	
	private Map<Integer, IMissionHandler> missionHandlerMap;
	public ArtifactDomain(){
		this.missionHandlerMap = new ConcurrentHashMap<>();
	}

	
	////////////业务代码////////////////////
	
	@Override
	public void afterInit() {
		beanMap.forEach((configId, Artifact) -> {
			//	伪代码,初始化任务处理器,不同的任务处理器代理的不同神器内的任务
			IMissionHandler handler = ArtifactMissionHandler.Builder.newBuilder()
					.playerId(playerId)
					.missionData(Artifact.getMissionData())
					.build();
			missionHandlerMap.put(configId, handler);
		});
	}
	
	@Override
	public ErrorCode onProcess(IEvent event) {
		ErrorCode code = ErrorCode.SUCCESS;
		for (Integer configId : missionHandlerMap.keySet()) {
			IMissionHandler handler = missionHandlerMap.get(configId);
			ErrorCode cur = handler.onProcess(event);
			if (cur.isSuccess()) {//	如果处理成功, 则保存对应的神兵对象
				getBean(configId).update();
			}else {
				code = cur;
			}
		}
		return code;
	}

	@Override
	public ErrorCode onReward(int configId, NatureEnum nenum) {
		IMissionHandler handler = missionHandlerMap.get(configId);
		return handler.onReward(configId, nenum);
	}

	@Override
	public Collection<PBMissionInfo> toProto() {
		Collection<PBMissionInfo> ret = new ArrayList<>();
		for (IMissionHandler handler : missionHandlerMap.values()) {
			ret.addAll(handler.toProto());
		}
		return ret;
	}

	@Override
	public List<IMission> getUpdateList() {
		List<IMission> ret = new ArrayList<>();
		for (IMissionHandler handler : missionHandlerMap.values()) {
			ret.addAll(handler.getUpdateList());
		}
		return ret;
	}
	
}

